package com.stkcall.library;

import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) company, All rights reserved.
 * <p>------------------------------------------------------
 *
 * @author: xujie
 * @date: 7/9/22
 */
public class AutoVoiceFileUtils {

    /**
     * 查询录音目录
     * @return
     */
    public static File pathFile() {
        String parentPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File childFile = null;
        if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("HUAWEI") || DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("HONOR")) {
            if (isExist(parentPath + "/Sounds/CallRecord")) {
                childFile = new File(parentPath + "/Sounds/CallRecord");
            } else if (isExist(parentPath + "/record")) {
                childFile = new File(parentPath + "record");
            } else if (isExist(parentPath + "/Record")) {
                childFile = new File(parentPath + "/Record");
            } else {
                childFile = null;
            }

        } else if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("XIAOMI")) {
            childFile = new File(parentPath + "/MIUI/sound_recorder/call_rec/");
        } else if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("MEIZU")) {
            childFile = new File(parentPath + "/Recorder");
        } else if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("OPPO")) {
            childFile = new File(parentPath + "/Music/Recordings/CallRecordings");
        } else if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("VIVO")) {
            childFile = new File(parentPath + "/Record/Call");
        } else if (DeviceUtils.getDeviceManufacturer().equalsIgnoreCase("SAMSUNG")) {
            childFile = new File(parentPath + "/Sounds");
        } else {
            childFile = null;
        }
        return childFile;
    }

    /**
     * 获取指定目录结构
     * @param dir
     * @return
     */
    public static File getRecordFile(String dir){
        String parentPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        return new File(parentPath + dir);
    }

    /*传入拨打的电话号码做匹配*/
    public static String getPathList(String phone) {
        ArrayList<String> result = new ArrayList<>();
        File[] files = pathFile().listFiles();
        if (files == null) {
            return "";
        }
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                String fileName = files[i].getName();
                if (fileName.trim().toLowerCase().contains(phone)) {
                    result.add(fileName);
                }
            }
        }

        return result.isEmpty() ? "" : result.get(0);
    }

    public static boolean isExist(String path){
        File file = new File(path);
        return file.exists();
    }
}
